// Package sys
// @Link  https://github.com/bufanyun/hotgo
// @Copyright  Copyright (c) 2024 HotGo CLI
// @Author  Ms <133814250@qq.com>
// @License  https://github.com/bufanyun/hotgo/blob/master/LICENSE
// @AutoGenerate Version 2.13.1
package sys

import (
	"context"
	"fmt"
	"github.com/gogf/gf/v2/os/gtime"
	"hotgo/internal/dao"
	"hotgo/internal/library/hgorm/handler"
	"hotgo/internal/model/input/form"
	"hotgo/internal/model/input/sysin"
	"hotgo/internal/service"
	"hotgo/utility/convert"
	"hotgo/utility/excel"
	"hotgo/utility/tradeDate"

	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/os/gctx"
	"github.com/gogf/gf/v2/util/gconv"
)

type sSysStockZtPool struct{}

func NewSysStockZtPool() *sSysStockZtPool {
	return &sSysStockZtPool{}
}

func init() {
	service.RegisterSysStockZtPool(NewSysStockZtPool())
}

// Model 涨停表现ORM模型
func (s *sSysStockZtPool) Model(ctx context.Context, option ...*handler.Option) *gdb.Model {
	return handler.Model(dao.StockZtPool.Ctx(ctx), option...)
}

// List 获取涨停表现列表
func (s *sSysStockZtPool) List(ctx context.Context, in *sysin.StockZtPoolListInp) (list []*sysin.StockZtPoolListModel, totalCount int, err error) {
	mod := s.Model(ctx)

	// 1 涨停 2 昨日涨停 3 跌停
	switch in.Type {
	case 1:
		latestDay := tradeDate.GetLatestDay()
		mod = mod.Where(dao.StockZtPool.Columns().Date, latestDay).
			WhereGT(dao.StockZtPool.Columns().ChangePercent, 0)
	case 2:
		latestDay := tradeDate.GetLatestDay()
		prevDate := tradeDate.GetStartTradeDay(latestDay, 1)
		mod = mod.Where(dao.StockAbnormalPool.Columns().Date, prevDate).
			WhereGT(dao.StockZtPool.Columns().ChangePercent, 0)
	case 3:
		latestDay := tradeDate.GetLatestDay()
		mod = mod.Where(dao.StockZtPool.Columns().Date, latestDay).
			WhereLT(dao.StockZtPool.Columns().ChangePercent, 0)
	default:
		if len(in.Date) != 0 {
			mod = mod.Where(dao.StockZtPool.Columns().Date, gtime.New(in.Date).Format("Y-m-d"))
		}
	}

	totalCount, err = mod.Clone().Count()
	if err != nil {
		err = gerror.Wrap(err, "获取涨停表现数据行失败，请稍后重试！")
		return
	}

	if totalCount == 0 {
		return
	}

	if err = mod.Fields(sysin.StockZtPoolListModel{}).Page(in.Page, in.PerPage).OrderDesc(dao.StockZtPool.Columns().Id).Scan(&list); err != nil {
		err = gerror.Wrap(err, "获取涨停表现列表失败，请稍后重试！")
		return
	}
	return
}

// Export 导出涨停表现
func (s *sSysStockZtPool) Export(ctx context.Context, in *sysin.StockZtPoolListInp) (err error) {
	list, totalCount, err := s.List(ctx, in)
	if err != nil {
		return
	}

	// 字段的排序是依据tags的字段顺序，如果你不想使用默认的排序方式，可以直接定义 tags = []string{"字段名称", "字段名称2", ...}
	tags, err := convert.GetEntityDescTags(sysin.StockZtPoolExportModel{})
	if err != nil {
		return
	}

	var (
		fileName  = "导出涨停表现-" + gctx.CtxId(ctx) + ".xlsx"
		sheetName = fmt.Sprintf("索引条件共%v行,共%v页,当前导出是第%v页,本页共%v行", totalCount, form.CalPageCount(totalCount, in.PerPage), in.Page, len(list))
		exports   []sysin.StockZtPoolExportModel
	)

	if err = gconv.Scan(list, &exports); err != nil {
		return
	}

	err = excel.ExportByStructs(ctx, tags, exports, fileName, sheetName)
	return
}

// Edit 修改/新增涨停表现
func (s *sSysStockZtPool) Edit(ctx context.Context, in *sysin.StockZtPoolEditInp) (err error) {
	var res *sysin.StockZtPoolListModel
	if err = s.Model(ctx).Where(dao.StockZtPool.Columns().Code, in.Code).Where(dao.StockZtPool.Columns().Date, in.Date).Scan(&res); err != nil {
		err = gerror.Wrap(err, "获取每日涨停数据失败，请稍后重试！")
		return err
	} else {
		if res == nil {
			// 新增
			if _, err = s.Model(ctx, &handler.Option{FilterAuth: false}).
				Fields(sysin.StockZtPoolInsertFields{}).Data(in).Insert(); err != nil {
				err = gerror.Wrap(err, "新增涨停表现失败，请稍后重试！")
			}
		} else if _, err = s.Model(ctx).Fields(sysin.StockZtPoolUpdateFields{}).
			Where(dao.StockZtPool.Columns().Code, in.Code).Where(dao.StockZtPool.Columns().Date).Data(in).Update(); err != nil {
			err = gerror.Wrap(err, "修改涨停表现失败，请稍后重试！")
		}
	}
	return
}

// Delete 删除涨停表现
func (s *sSysStockZtPool) Delete(ctx context.Context, in *sysin.StockZtPoolDeleteInp) (err error) {
	if _, err = s.Model(ctx).Where(dao.StockZtPool.Columns().Date, in.Date).Delete(); err != nil {
		err = gerror.Wrap(err, "删除涨停表现失败，请稍后重试！")
		return
	}
	return
}

// View 获取涨停表现指定信息
func (s *sSysStockZtPool) View(ctx context.Context, in *sysin.StockZtPoolViewInp) (res *sysin.StockZtPoolViewModel, err error) {
	if err = s.Model(ctx).WherePri(in.Id).Scan(&res); err != nil {
		err = gerror.Wrap(err, "获取涨停表现信息，请稍后重试！")
		return
	}
	return
}
